home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.4)
-
- '''Tests for cookielib.py.'''
- import re
- import os
- import time
- from unittest import TestCase
- from test import test_support
-
- class DateTimeTests(TestCase):
-
- def test_time2isoz(self):
- time2isoz = time2isoz
- import cookielib
- base = 1019227000
- day = 24 * 3600
- self.assertEquals(time2isoz(base), '2002-04-19 14:36:40Z')
- self.assertEquals(time2isoz(base + day), '2002-04-20 14:36:40Z')
- self.assertEquals(time2isoz(base + 2 * day), '2002-04-21 14:36:40Z')
- self.assertEquals(time2isoz(base + 3 * day), '2002-04-22 14:36:40Z')
- az = time2isoz()
- bz = time2isoz(500000)
- for text in (az, bz):
- self.assert_(re.search('^\\d{4}-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\dZ$', text), 'bad time2isoz format: %s %s' % (az, bz))
-
-
-
- def test_http2time(self):
- http2time = http2time
- import cookielib
-
- def parse_date(text):
- return time.gmtime(http2time(text))[:6]
-
- self.assertEquals(parse_date('01 Jan 2001'), (2001, 1, 1, 0, 0, 0.0))
- self.assertEquals(parse_date('03-Feb-20'), (2020, 2, 3, 0, 0, 0.0))
- self.assertEquals(parse_date('03-Feb-98'), (1998, 2, 3, 0, 0, 0.0))
-
-
- def test_http2time_formats(self):
- http2time = http2time
- time2isoz = time2isoz
- import cookielib
- tests = [
- 'Thu, 03 Feb 1994 00:00:00 GMT',
- 'Thursday, 03-Feb-94 00:00:00 GMT',
- 'Thursday, 03-Feb-1994 00:00:00 GMT',
- '03 Feb 1994 00:00:00 GMT',
- '03-Feb-94 00:00:00 GMT',
- '03-Feb-1994 00:00:00 GMT',
- '03-Feb-1994 00:00 GMT',
- '03-Feb-1994 00:00',
- '03-Feb-94',
- '03-Feb-1994',
- '03 Feb 1994',
- ' 03 Feb 1994 0:00 ',
- ' 03-Feb-1994 ']
- test_t = 760233600
- result = time2isoz(test_t)
- expected = '1994-02-03 00:00:00Z'
- self.assertEquals(result, expected, "%s => '%s' (%s)" % (test_t, result, expected))
- for s in tests:
- t = http2time(s)
- t2 = http2time(s.lower())
- t3 = http2time(s.upper())
- None(self.assert_ if t2 == t2 and t3 == t3 else t3 == test_t, "'%s' => %s, %s, %s (%s)" % (s, t, t2, t3, test_t))
-
-
-
- def test_http2time_garbage(self):
- http2time = http2time
- import cookielib
- for test in [
- '',
- 'Garbage',
- 'Mandag 16. September 1996',
- '01-00-1980',
- '01-13-1980',
- '00-01-1980',
- '32-01-1980',
- '01-01-1980 25:00:00',
- '01-01-1980 00:61:00',
- '01-01-1980 00:00:62']:
- self.assert_(http2time(test) is None, 'http2time(%s) is not None\nhttp2time(test) %s' % (test, http2time(test)))
-
-
-
-
- class HeaderTests(TestCase):
-
- def test_parse_ns_headers(self):
- parse_ns_headers = parse_ns_headers
- import cookielib
- expected = [
- [
- ('foo', 'bar'),
- ('expires', 0x83ABB964L),
- ('version', '0')]]
- for hdr in [
- 'foo=bar; expires=01 Jan 2040 22:23:32 GMT',
- 'foo=bar; expires="01 Jan 2040 22:23:32 GMT"']:
- self.assertEquals(parse_ns_headers([
- hdr]), expected)
-
-
-
- def test_parse_ns_headers_special_names(self):
- parse_ns_headers = parse_ns_headers
- import cookielib
- hdr = 'expires=01 Jan 2040 22:23:32 GMT'
- expected = [
- [
- ('expires', '01 Jan 2040 22:23:32 GMT'),
- ('version', '0')]]
- self.assertEquals(parse_ns_headers([
- hdr]), expected)
-
-
- def test_join_header_words(self):
- join_header_words = join_header_words
- import cookielib
- joined = join_header_words([
- [
- ('foo', None),
- ('bar', 'baz')]])
- self.assertEquals(joined, 'foo; bar=baz')
- self.assertEquals(join_header_words([
- []]), '')
-
-
- def test_split_header_words(self):
- split_header_words = split_header_words
- import cookielib
- tests = [
- ('foo', [
- [
- ('foo', None)]]),
- ('foo=bar', [
- [
- ('foo', 'bar')]]),
- (' foo ', [
- [
- ('foo', None)]]),
- (' foo= ', [
- [
- ('foo', '')]]),
- (' foo=', [
- [
- ('foo', '')]]),
- (' foo= ; ', [
- [
- ('foo', '')]]),
- (' foo= ; bar= baz ', [
- [
- ('foo', ''),
- ('bar', 'baz')]]),
- ('foo=bar bar=baz', [
- [
- ('foo', 'bar'),
- ('bar', 'baz')]]),
- ('foo= bar=baz', [
- [
- ('foo', 'bar=baz')]]),
- ('foo=bar;bar=baz', [
- [
- ('foo', 'bar'),
- ('bar', 'baz')]]),
- ('foo bar baz', [
- [
- ('foo', None),
- ('bar', None),
- ('baz', None)]]),
- ('a, b, c', [
- [
- ('a', None)],
- [
- ('b', None)],
- [
- ('c', None)]]),
- ('foo; bar=baz, spam=, foo="\\,\\;\\"", bar= ', [
- [
- ('foo', None),
- ('bar', 'baz')],
- [
- ('spam', '')],
- [
- ('foo', ',;"')],
- [
- ('bar', '')]])]
- for arg, expect in tests:
-
- try:
- result = split_header_words([
- arg])
- except:
- import traceback
- import StringIO
- f = StringIO.StringIO()
- traceback.print_exc(None, f)
- result = '(error -- traceback follows)\n\n%s' % f.getvalue()
-
- self.assertEquals(result, expect, "\nWhen parsing: '%s'\nExpected: '%s'\nGot: '%s'\n" % (arg, expect, result))
-
-
-
- def test_roundtrip(self):
- split_header_words = split_header_words
- join_header_words = join_header_words
- import cookielib
- tests = [
- ('foo', 'foo'),
- ('foo=bar', 'foo=bar'),
- (' foo ', 'foo'),
- ('foo=', 'foo=""'),
- ('foo=bar bar=baz', 'foo=bar; bar=baz'),
- ('foo=bar;bar=baz', 'foo=bar; bar=baz'),
- ('foo bar baz', 'foo; bar; baz'),
- ('foo="\\"" bar="\\\\"', 'foo="\\""; bar="\\\\"'),
- ('foo,,,bar', 'foo, bar'),
- ('foo=bar,bar=baz', 'foo=bar, bar=baz'),
- ('text/html; charset=iso-8859-1', 'text/html; charset="iso-8859-1"'),
- ('foo="bar"; port="80,81"; discard, bar=baz', 'foo=bar; port="80,81"; discard, bar=baz'),
- ('Basic realm="\\"foo\\\\\\\\bar\\""', 'Basic; realm="\\"foo\\\\\\\\bar\\""')]
- for arg, expect in tests:
- input = split_header_words([
- arg])
- res = join_header_words(input)
- self.assertEquals(res, expect, "\nWhen parsing: '%s'\nExpected: '%s'\nGot: '%s'\nInput was: '%s'\n" % (arg, expect, res, input))
-
-
-
-
- class FakeResponse:
-
- def __init__(self, headers = [], url = None):
- """
- headers: list of RFC822-style 'Key: value' strings
- """
- import mimetools
- import StringIO
- f = StringIO.StringIO('\n'.join(headers))
- self._headers = mimetools.Message(f)
- self._url = url
-
-
- def info(self):
- return self._headers
-
-
-
- def interact_2965(cookiejar, url, *set_cookie_hdrs):
- return _interact(cookiejar, url, set_cookie_hdrs, 'Set-Cookie2')
-
-
- def interact_netscape(cookiejar, url, *set_cookie_hdrs):
- return _interact(cookiejar, url, set_cookie_hdrs, 'Set-Cookie')
-
-
- def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):
- '''Perform a single request / response cycle, returning Cookie: header.'''
- Request = Request
- import urllib2
- req = Request(url)
- cookiejar.add_cookie_header(req)
- cookie_hdr = req.get_header('Cookie', '')
- headers = []
- for hdr in set_cookie_hdrs:
- headers.append('%s: %s' % (hdr_name, hdr))
-
- res = FakeResponse(headers, url)
- cookiejar.extract_cookies(res, req)
- return cookie_hdr
-
-
- class FileCookieJarTests(TestCase):
-
- def test_lwp_valueless_cookie(self):
- LWPCookieJar = LWPCookieJar
- import cookielib
- filename = test_support.TESTFN
- c = LWPCookieJar()
- interact_netscape(c, 'http://www.acme.com/', 'boo')
- self.assertEqual(c._cookies['www.acme.com']['/']['boo'].value, None)
-
- try:
- c.save(filename, ignore_discard = True)
- c = LWPCookieJar()
- c.load(filename, ignore_discard = True)
- finally:
-
- try:
- os.unlink(filename)
- except OSError:
- pass
-
-
- self.assertEqual(c._cookies['www.acme.com']['/']['boo'].value, None)
-
-
-
- class CookieTests(TestCase):
-
- def test_domain_return_ok(self):
- import cookielib
- import urllib2
- pol = cookielib.DefaultCookiePolicy()
- for url, domain, ok in [
- ('http://foo.bar.com/', 'blah.com', False),
- ('http://foo.bar.com/', 'rhubarb.blah.com', False),
- ('http://foo.bar.com/', 'rhubarb.foo.bar.com', False),
- ('http://foo.bar.com/', '.foo.bar.com', True),
- ('http://foo.bar.com/', 'foo.bar.com', True),
- ('http://foo.bar.com/', '.bar.com', True),
- ('http://foo.bar.com/', 'com', True),
- ('http://foo.com/', 'rhubarb.foo.com', False),
- ('http://foo.com/', '.foo.com', True),
- ('http://foo.com/', 'foo.com', True),
- ('http://foo.com/', 'com', True),
- ('http://foo/', 'rhubarb.foo', False),
- ('http://foo/', '.foo', True),
- ('http://foo/', 'foo', True),
- ('http://foo/', 'foo.local', True),
- ('http://foo/', '.local', True)]:
- request = urllib2.Request(url)
- r = pol.domain_return_ok(domain, request)
- if ok:
- self.assert_(r)
- continue
- self.assert_(not r)
-
-
-
- def test_missing_value(self):
- MozillaCookieJar = MozillaCookieJar
- lwp_cookie_str = lwp_cookie_str
- import cookielib
- filename = test_support.TESTFN
- c = MozillaCookieJar(filename)
- interact_netscape(c, 'http://www.acme.com/', 'eggs')
- interact_netscape(c, 'http://www.acme.com/', '"spam"; path=/foo/')
- cookie = c._cookies['www.acme.com']['/']['eggs']
- self.assert_(cookie.value is None)
- self.assertEquals(cookie.name, 'eggs')
- cookie = c._cookies['www.acme.com']['/foo/']['"spam"']
- self.assert_(cookie.value is None)
- self.assertEquals(cookie.name, '"spam"')
- self.assertEquals(lwp_cookie_str(cookie), '"spam"; path="/foo/"; domain="www.acme.com"; path_spec; discard; version=0')
- old_str = repr(c)
- c.save(ignore_expires = True, ignore_discard = True)
-
- try:
- c = MozillaCookieJar(filename)
- c.revert(ignore_expires = True, ignore_discard = True)
- finally:
- os.unlink(c.filename)
-
- self.assertEquals(repr(c), re.sub('path_specified=%s' % True, 'path_specified=%s' % False, old_str))
- self.assertEquals(interact_netscape(c, 'http://www.acme.com/foo/'), '"spam"; eggs')
-
-
- def test_ns_parser(self):
- CookieJar = CookieJar
- DEFAULT_HTTP_PORT = DEFAULT_HTTP_PORT
- import cookielib
- c = CookieJar()
- interact_netscape(c, 'http://www.acme.com/', 'spam=eggs; DoMain=.acme.com; port; blArgh="feep"')
- interact_netscape(c, 'http://www.acme.com/', 'ni=ni; port=80,8080')
- interact_netscape(c, 'http://www.acme.com:80/', 'nini=ni')
- interact_netscape(c, 'http://www.acme.com:80/', 'foo=bar; expires=')
- interact_netscape(c, 'http://www.acme.com:80/', 'spam=eggs; expires="Foo Bar 25 33:22:11 3022"')
- cookie = c._cookies['.acme.com']['/']['spam']
- self.assertEquals(cookie.domain, '.acme.com')
- self.assert_(cookie.domain_specified)
- self.assertEquals(cookie.port, DEFAULT_HTTP_PORT)
- self.assert_(not (cookie.port_specified))
- if cookie.has_nonstandard_attr('blArgh'):
- pass
- self.assert_(not cookie.has_nonstandard_attr('blargh'))
- cookie = c._cookies['www.acme.com']['/']['ni']
- self.assertEquals(cookie.domain, 'www.acme.com')
- self.assert_(not (cookie.domain_specified))
- self.assertEquals(cookie.port, '80,8080')
- self.assert_(cookie.port_specified)
- cookie = c._cookies['www.acme.com']['/']['nini']
- self.assert_(cookie.port is None)
- self.assert_(not (cookie.port_specified))
- foo = c._cookies['www.acme.com']['/']['foo']
- spam = c._cookies['www.acme.com']['/']['foo']
- self.assert_(foo.expires is None)
- self.assert_(spam.expires is None)
-
-
- def test_ns_parser_special_names(self):
- CookieJar = CookieJar
- import cookielib
- c = CookieJar()
- interact_netscape(c, 'http://www.acme.com/', 'expires=eggs')
- interact_netscape(c, 'http://www.acme.com/', 'version=eggs; spam=eggs')
- cookies = c._cookies['www.acme.com']['/']
- self.assert_('expires' in cookies)
- self.assert_('version' in cookies)
-
-
- def test_expires(self):
- time2netscape = time2netscape
- CookieJar = CookieJar
- import cookielib
- c = CookieJar()
- future = time2netscape(time.time() + 3600)
- interact_netscape(c, 'http://www.acme.com/', 'spam="bar"; expires=%s' % future)
- self.assertEquals(len(c), 1)
- now = time2netscape(time.time() - 1)
- interact_netscape(c, 'http://www.acme.com/', 'foo="eggs"; expires=%s' % now)
- h = interact_netscape(c, 'http://www.acme.com/')
- self.assertEquals(len(c), 1)
- if 'spam="bar"' in h:
- pass
- self.assert_('foo' not in h)
- interact_netscape(c, 'http://www.acme.com/', 'eggs="bar"; expires=%s' % future)
- interact_netscape(c, 'http://www.acme.com/', 'bar="bar"; expires=%s' % future)
- self.assertEquals(len(c), 3)
- interact_netscape(c, 'http://www.acme.com/', 'eggs="bar"; expires=%s; max-age=0' % future)
- interact_netscape(c, 'http://www.acme.com/', 'bar="bar"; max-age=0; expires=%s' % future)
- h = interact_netscape(c, 'http://www.acme.com/')
- self.assertEquals(len(c), 1)
- interact_netscape(c, 'http://www.rhubarb.net/', 'whum="fizz"')
- self.assertEquals(len(c), 2)
- c.clear_session_cookies()
- self.assertEquals(len(c), 1)
- self.assert_('spam="bar"' in h)
-
-
- def test_default_path(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- pol = DefaultCookiePolicy(rfc2965 = True)
- c = CookieJar(pol)
- interact_2965(c, 'http://www.acme.com/', 'spam="bar"; Version="1"')
- self.assert_('/' in c._cookies['www.acme.com'])
- c = CookieJar(pol)
- interact_2965(c, 'http://www.acme.com/blah', 'eggs="bar"; Version="1"')
- self.assert_('/' in c._cookies['www.acme.com'])
- c = CookieJar(pol)
- interact_2965(c, 'http://www.acme.com/blah/rhubarb', 'eggs="bar"; Version="1"')
- self.assert_('/blah/' in c._cookies['www.acme.com'])
- c = CookieJar(pol)
- interact_2965(c, 'http://www.acme.com/blah/rhubarb/', 'eggs="bar"; Version="1"')
- self.assert_('/blah/rhubarb/' in c._cookies['www.acme.com'])
- c = CookieJar()
- interact_netscape(c, 'http://www.acme.com/', 'spam="bar"')
- self.assert_('/' in c._cookies['www.acme.com'])
- c = CookieJar()
- interact_netscape(c, 'http://www.acme.com/blah', 'eggs="bar"')
- self.assert_('/' in c._cookies['www.acme.com'])
- c = CookieJar()
- interact_netscape(c, 'http://www.acme.com/blah/rhubarb', 'eggs="bar"')
- self.assert_('/blah' in c._cookies['www.acme.com'])
- c = CookieJar()
- interact_netscape(c, 'http://www.acme.com/blah/rhubarb/', 'eggs="bar"')
- self.assert_('/blah/rhubarb' in c._cookies['www.acme.com'])
-
-
- def test_escape_path(self):
- escape_path = escape_path
- import cookielib
- cases = [
- ('/foo%2f/bar', '/foo%2F/bar'),
- ('/foo%2F/bar', '/foo%2F/bar'),
- ('/foo%%/bar', '/foo%%/bar'),
- ('/fo%19o/bar', '/fo%19o/bar'),
- ('/fo%7do/bar', '/fo%7Do/bar'),
- ('/foo/bar&', '/foo/bar&'),
- ('/foo//bar', '/foo//bar'),
- ('~/foo/bar', '~/foo/bar'),
- ('/foo\x19/bar', '/foo%19/bar'),
- ('/}foo/bar', '/%7Dfoo/bar'),
- (u'/foo/barꯍ', '/foo/bar%EA%AF%8D')]
- for arg, result in cases:
- self.assertEquals(escape_path(arg), result)
-
-
-
- def test_request_path(self):
- Request = Request
- import urllib2
- request_path = request_path
- import cookielib
- req = Request('http://www.example.com/rheum/rhaponicum;foo=bar;sing=song?apples=pears&spam=eggs#ni')
- self.assertEquals(request_path(req), '/rheum/rhaponicum;foo=bar;sing=song?apples=pears&spam=eggs#ni')
- req = Request('http://www.example.com/rheum/rhaponicum?apples=pears&spam=eggs#ni')
- self.assertEquals(request_path(req), '/rheum/rhaponicum?apples=pears&spam=eggs#ni')
- req = Request('http://www.example.com')
- self.assertEquals(request_path(req), '/')
-
-
- def test_request_port(self):
- Request = Request
- import urllib2
- request_port = request_port
- DEFAULT_HTTP_PORT = DEFAULT_HTTP_PORT
- import cookielib
- req = Request('http://www.acme.com:1234/', headers = {
- 'Host': 'www.acme.com:4321' })
- self.assertEquals(request_port(req), '1234')
- req = Request('http://www.acme.com/', headers = {
- 'Host': 'www.acme.com:4321' })
- self.assertEquals(request_port(req), DEFAULT_HTTP_PORT)
-
-
- def test_request_host(self):
- Request = Request
- import urllib2
- request_host = request_host
- import cookielib
- req = Request('http://1.1.1.1/', headers = {
- 'Host': 'www.acme.com:80' })
- self.assertEquals(request_host(req), '1.1.1.1')
- req = Request('http://www.acme.com/', headers = {
- 'Host': 'irrelevant.com' })
- self.assertEquals(request_host(req), 'www.acme.com')
- req = Request('/resource.html', headers = {
- 'Host': 'www.acme.com' })
- self.assertEquals(request_host(req), 'www.acme.com')
- req = Request('http://www.acme.com:2345/resource.html', headers = {
- 'Host': 'www.acme.com:5432' })
- self.assertEquals(request_host(req), 'www.acme.com')
-
-
- def test_is_HDN(self):
- is_HDN = is_HDN
- import cookielib
- self.assert_(is_HDN('foo.bar.com'))
- self.assert_(is_HDN('1foo2.3bar4.5com'))
- self.assert_(not is_HDN('192.168.1.1'))
- self.assert_(not is_HDN(''))
- self.assert_(not is_HDN('.'))
- self.assert_(not is_HDN('.foo.bar.com'))
- self.assert_(not is_HDN('..foo'))
- self.assert_(not is_HDN('foo.'))
-
-
- def test_reach(self):
- reach = reach
- import cookielib
- self.assertEquals(reach('www.acme.com'), '.acme.com')
- self.assertEquals(reach('acme.com'), 'acme.com')
- self.assertEquals(reach('acme.local'), '.local')
- self.assertEquals(reach('.local'), '.local')
- self.assertEquals(reach('.com'), '.com')
- self.assertEquals(reach('.'), '.')
- self.assertEquals(reach(''), '')
- self.assertEquals(reach('192.168.0.1'), '192.168.0.1')
-
-
- def test_domain_match(self):
- domain_match = domain_match
- user_domain_match = user_domain_match
- import cookielib
- self.assert_(domain_match('192.168.1.1', '192.168.1.1'))
- self.assert_(not domain_match('192.168.1.1', '.168.1.1'))
- self.assert_(domain_match('x.y.com', 'x.Y.com'))
- self.assert_(domain_match('x.y.com', '.Y.com'))
- self.assert_(not domain_match('x.y.com', 'Y.com'))
- self.assert_(domain_match('a.b.c.com', '.c.com'))
- self.assert_(not domain_match('.c.com', 'a.b.c.com'))
- self.assert_(domain_match('example.local', '.local'))
- self.assert_(not domain_match('blah.blah', ''))
- self.assert_(not domain_match('', '.rhubarb.rhubarb'))
- self.assert_(domain_match('', ''))
- self.assert_(user_domain_match('acme.com', 'acme.com'))
- self.assert_(not user_domain_match('acme.com', '.acme.com'))
- self.assert_(user_domain_match('rhubarb.acme.com', '.acme.com'))
- self.assert_(user_domain_match('www.rhubarb.acme.com', '.acme.com'))
- self.assert_(user_domain_match('x.y.com', 'x.Y.com'))
- self.assert_(user_domain_match('x.y.com', '.Y.com'))
- self.assert_(not user_domain_match('x.y.com', 'Y.com'))
- self.assert_(user_domain_match('y.com', 'Y.com'))
- self.assert_(not user_domain_match('.y.com', 'Y.com'))
- self.assert_(user_domain_match('.y.com', '.Y.com'))
- self.assert_(user_domain_match('x.y.com', '.com'))
- self.assert_(not user_domain_match('x.y.com', 'com'))
- self.assert_(not user_domain_match('x.y.com', 'm'))
- self.assert_(not user_domain_match('x.y.com', '.m'))
- self.assert_(not user_domain_match('x.y.com', ''))
- self.assert_(not user_domain_match('x.y.com', '.'))
- self.assert_(user_domain_match('192.168.1.1', '192.168.1.1'))
- self.assert_(not user_domain_match('192.168.1.1', '.168.1.1'))
- self.assert_(not user_domain_match('192.168.1.1', '.'))
- self.assert_(not user_domain_match('192.168.1.1', ''))
-
-
- def test_wrong_domain(self):
- CookieJar = CookieJar
- import cookielib
- c = CookieJar()
- interact_2965(c, 'http://www.nasty.com/', 'foo=bar; domain=friendly.org; Version="1"')
- self.assertEquals(len(c), 0)
-
-
- def test_two_component_domain_ns(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- c = CookieJar()
- interact_netscape(c, 'http://foo.net/', 'ns=bar')
- self.assertEquals(len(c), 1)
- self.assertEquals(c._cookies['foo.net']['/']['ns'].value, 'bar')
- self.assertEquals(interact_netscape(c, 'http://foo.net/'), 'ns=bar')
- self.assertEquals(interact_netscape(c, 'http://www.foo.net/'), 'ns=bar')
- pol = DefaultCookiePolicy(strict_ns_domain = DefaultCookiePolicy.DomainStrictNonDomain)
- c.set_policy(pol)
- self.assertEquals(interact_netscape(c, 'http://www.foo.net/'), '')
- interact_netscape(c, 'http://foo.net/foo/', 'spam1=eggs; domain=foo.net')
- interact_netscape(c, 'http://foo.net/foo/bar/', 'spam2=eggs; domain=.foo.net')
- self.assertEquals(len(c), 3)
- self.assertEquals(c._cookies['.foo.net']['/foo']['spam1'].value, 'eggs')
- self.assertEquals(c._cookies['.foo.net']['/foo/bar']['spam2'].value, 'eggs')
- self.assertEquals(interact_netscape(c, 'http://foo.net/foo/bar/'), 'spam2=eggs; spam1=eggs; ns=bar')
- interact_netscape(c, 'http://foo.net/', 'nini="ni"; domain=.net')
- self.assertEquals(len(c), 3)
- interact_netscape(c, 'http://foo.co.uk', 'nasty=trick; domain=.co.uk')
- self.assertEquals(len(c), 4)
-
-
- def test_two_component_domain_rfc2965(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- pol = DefaultCookiePolicy(rfc2965 = True)
- c = CookieJar(pol)
- interact_2965(c, 'http://foo.net/', 'foo=bar; Version="1"')
- self.assertEquals(len(c), 1)
- self.assertEquals(c._cookies['foo.net']['/']['foo'].value, 'bar')
- self.assertEquals(interact_2965(c, 'http://foo.net/'), '$Version=1; foo=bar')
- self.assertEquals(interact_2965(c, 'http://www.foo.net/'), '')
- interact_2965(c, 'http://foo.net/foo', 'spam=eggs; domain=foo.net; path=/foo; Version="1"')
- self.assertEquals(len(c), 1)
- self.assertEquals(interact_2965(c, 'http://foo.net/foo'), '$Version=1; foo=bar')
- interact_2965(c, 'http://www.foo.net/foo/', 'spam=eggs; domain=foo.net; Version="1"')
- self.assertEquals(c._cookies['.foo.net']['/foo/']['spam'].value, 'eggs')
- self.assertEquals(len(c), 2)
- self.assertEquals(interact_2965(c, 'http://foo.net/foo/'), '$Version=1; foo=bar')
- self.assertEquals(interact_2965(c, 'http://www.foo.net/foo/'), '$Version=1; spam=eggs; $Domain="foo.net"')
- interact_2965(c, 'http://foo.net/', 'ni="ni"; domain=".net"; Version="1"')
- self.assertEquals(len(c), 2)
- interact_2965(c, 'http://foo.co.uk/', 'nasty=trick; domain=.co.uk; Version="1"')
- self.assertEquals(len(c), 3)
-
-
- def test_domain_allow(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- Request = Request
- import urllib2
- c = CookieJar(policy = DefaultCookiePolicy(blocked_domains = [
- 'acme.com'], allowed_domains = [
- 'www.acme.com']))
- req = Request('http://acme.com/')
- headers = [
- 'Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/']
- res = FakeResponse(headers, 'http://acme.com/')
- c.extract_cookies(res, req)
- self.assertEquals(len(c), 0)
- req = Request('http://www.acme.com/')
- res = FakeResponse(headers, 'http://www.acme.com/')
- c.extract_cookies(res, req)
- self.assertEquals(len(c), 1)
- req = Request('http://www.coyote.com/')
- res = FakeResponse(headers, 'http://www.coyote.com/')
- c.extract_cookies(res, req)
- self.assertEquals(len(c), 1)
- req = Request('http://www.coyote.com/')
- res = FakeResponse(headers, 'http://www.coyote.com/')
- cookies = c.make_cookies(res, req)
- c.set_cookie(cookies[0])
- self.assertEquals(len(c), 2)
- c.add_cookie_header(req)
- self.assert_(not req.has_header('Cookie'))
-
-
- def test_domain_block(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- Request = Request
- import urllib2
- pol = DefaultCookiePolicy(rfc2965 = True, blocked_domains = [
- '.acme.com'])
- c = CookieJar(policy = pol)
- headers = [
- 'Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/']
- req = Request('http://www.acme.com/')
- res = FakeResponse(headers, 'http://www.acme.com/')
- c.extract_cookies(res, req)
- self.assertEquals(len(c), 0)
- p = pol.set_blocked_domains([
- 'acme.com'])
- c.extract_cookies(res, req)
- self.assertEquals(len(c), 1)
- c.clear()
- req = Request('http://www.roadrunner.net/')
- res = FakeResponse(headers, 'http://www.roadrunner.net/')
- c.extract_cookies(res, req)
- self.assertEquals(len(c), 1)
- req = Request('http://www.roadrunner.net/')
- c.add_cookie_header(req)
- if req.has_header('Cookie'):
- pass
- self.assert_(req.has_header('Cookie2'))
- c.clear()
- pol.set_blocked_domains([
- '.acme.com'])
- c.extract_cookies(res, req)
- self.assertEquals(len(c), 1)
- req = Request('http://www.acme.com/')
- res = FakeResponse(headers, 'http://www.acme.com/')
- cookies = c.make_cookies(res, req)
- c.set_cookie(cookies[0])
- self.assertEquals(len(c), 2)
- c.add_cookie_header(req)
- self.assert_(not req.has_header('Cookie'))
-
-
- def test_secure(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- for ns in (True, False):
- for whitespace in (' ', ''):
- c = CookieJar()
- if ns:
- pol = DefaultCookiePolicy(rfc2965 = False)
- int = interact_netscape
- vs = ''
- else:
- pol = DefaultCookiePolicy(rfc2965 = True)
- int = interact_2965
- vs = '; Version=1'
- c.set_policy(pol)
- url = 'http://www.acme.com/'
- int(c, url, 'foo1=bar%s%s' % (vs, whitespace))
- int(c, url, 'foo2=bar%s; secure%s' % (vs, whitespace))
- self.assert_(not (c._cookies['www.acme.com']['/']['foo1'].secure), 'non-secure cookie registered secure')
- self.assert_(c._cookies['www.acme.com']['/']['foo2'].secure, 'secure cookie registered non-secure')
-
-
-
-
- def test_quote_cookie_value(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- c = CookieJar(policy = DefaultCookiePolicy(rfc2965 = True))
- interact_2965(c, 'http://www.acme.com/', 'foo=\\b"a"r; Version=1')
- h = interact_2965(c, 'http://www.acme.com/')
- self.assertEquals(h, '$Version=1; foo=\\\\b\\"a\\"r')
-
-
- def test_missing_final_slash(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- Request = Request
- import urllib2
- url = 'http://www.acme.com'
- c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
- interact_2965(c, url, 'foo=bar; Version=1')
- req = Request(url)
- self.assertEquals(len(c), 1)
- c.add_cookie_header(req)
- self.assert_(req.has_header('Cookie'))
-
-
- def test_domain_mirror(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- pol = DefaultCookiePolicy(rfc2965 = True)
- c = CookieJar(pol)
- url = 'http://foo.bar.com/'
- interact_2965(c, url, 'spam=eggs; Version=1')
- h = interact_2965(c, url)
- self.assert_('Domain' not in h, 'absent domain returned with domain present')
- c = CookieJar(pol)
- url = 'http://foo.bar.com/'
- interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com')
- h = interact_2965(c, url)
- self.assert_('$Domain=".bar.com"' in h, 'domain not returned')
- c = CookieJar(pol)
- url = 'http://foo.bar.com/'
- interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com')
- h = interact_2965(c, url)
- self.assert_('$Domain="bar.com"' in h, 'domain not returned')
-
-
- def test_path_mirror(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- pol = DefaultCookiePolicy(rfc2965 = True)
- c = CookieJar(pol)
- url = 'http://foo.bar.com/'
- interact_2965(c, url, 'spam=eggs; Version=1')
- h = interact_2965(c, url)
- self.assert_('Path' not in h, 'absent path returned with path present')
- c = CookieJar(pol)
- url = 'http://foo.bar.com/'
- interact_2965(c, url, 'spam=eggs; Version=1; Path=/')
- h = interact_2965(c, url)
- self.assert_('$Path="/"' in h, 'path not returned')
-
-
- def test_port_mirror(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- pol = DefaultCookiePolicy(rfc2965 = True)
- c = CookieJar(pol)
- url = 'http://foo.bar.com/'
- interact_2965(c, url, 'spam=eggs; Version=1')
- h = interact_2965(c, url)
- self.assert_('Port' not in h, 'absent port returned with port present')
- c = CookieJar(pol)
- url = 'http://foo.bar.com/'
- interact_2965(c, url, 'spam=eggs; Version=1; Port')
- h = interact_2965(c, url)
- self.assert_(re.search('\\$Port([^=]|$)', h), 'port with no value not returned with no value')
- c = CookieJar(pol)
- url = 'http://foo.bar.com/'
- interact_2965(c, url, 'spam=eggs; Version=1; Port="80"')
- h = interact_2965(c, url)
- self.assert_('$Port="80"' in h, 'port with single value not returned with single value')
- c = CookieJar(pol)
- url = 'http://foo.bar.com/'
- interact_2965(c, url, 'spam=eggs; Version=1; Port="80,8080"')
- h = interact_2965(c, url)
- self.assert_('$Port="80,8080"' in h, 'port with multiple values not returned with multiple values')
-
-
- def test_no_return_comment(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
- url = 'http://foo.bar.com/'
- interact_2965(c, url, 'spam=eggs; Version=1; Comment="does anybody read these?"; CommentURL="http://foo.bar.net/comment.html"')
- h = interact_2965(c, url)
- self.assert_('Comment' not in h, 'Comment or CommentURL cookie-attributes returned to server')
-
-
- def test_Cookie_iterator(self):
- CookieJar = CookieJar
- Cookie = Cookie
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- cs = CookieJar(DefaultCookiePolicy(rfc2965 = True))
- interact_2965(cs, 'http://blah.spam.org/', 'foo=eggs; Version=1; Comment="does anybody read these?"; CommentURL="http://foo.bar.net/comment.html"')
- interact_netscape(cs, 'http://www.acme.com/blah/', 'spam=bar; secure')
- interact_2965(cs, 'http://www.acme.com/blah/', 'foo=bar; secure; Version=1')
- interact_2965(cs, 'http://www.acme.com/blah/', 'foo=bar; path=/; Version=1')
- interact_2965(cs, 'http://www.sol.no', 'bang=wallop; version=1; domain=".sol.no"; port="90,100, 80,8080"; max-age=100; Comment = "Just kidding! (\\"|\\\\\\\\) "')
- versions = [
- 1,
- 1,
- 1,
- 0,
- 1]
- names = [
- 'bang',
- 'foo',
- 'foo',
- 'spam',
- 'foo']
- domains = [
- '.sol.no',
- 'blah.spam.org',
- 'www.acme.com',
- 'www.acme.com',
- 'www.acme.com']
- paths = [
- '/',
- '/',
- '/',
- '/blah',
- '/blah/']
- for i in range(4):
- i = 0
- for c in cs:
- self.assert_(isinstance(c, Cookie))
- self.assertEquals(c.version, versions[i])
- self.assertEquals(c.name, names[i])
- self.assertEquals(c.domain, domains[i])
- self.assertEquals(c.path, paths[i])
- i = i + 1
-
-
-
-
- def test_parse_ns_headers(self):
- parse_ns_headers = parse_ns_headers
- import cookielib
- self.assertEquals(parse_ns_headers([
- 'foo=bar; path=/; domain']), [
- [
- ('foo', 'bar'),
- ('path', '/'),
- ('domain', None),
- ('version', '0')]])
- self.assertEquals(parse_ns_headers([
- 'foo=bar; expires=Foo Bar 12 33:22:11 2000']), [
- [
- ('foo', 'bar'),
- ('expires', None),
- ('version', '0')]])
- self.assertEquals(parse_ns_headers([
- 'foo']), [
- [
- ('foo', None),
- ('version', '0')]])
- self.assertEquals(parse_ns_headers([
- '']), [])
-
-
- def test_bad_cookie_header(self):
-
- def cookiejar_from_cookie_headers(headers):
- CookieJar = CookieJar
- import cookielib
- Request = Request
- import urllib2
- c = CookieJar()
- req = Request('http://www.example.com/')
- r = FakeResponse(headers, 'http://www.example.com/')
- c.extract_cookies(r, req)
- return c
-
- for headers in [
- [
- 'Set-Cookie: '],
- [
- 'Set-Cookie2: '],
- [
- 'Set-Cookie2: a=foo; path=/; Version=1; domain'],
- [
- 'Set-Cookie: b=foo; max-age=oops']]:
- c = cookiejar_from_cookie_headers(headers)
- self.assertEquals(len(c), 0)
-
- headers = [
- 'Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000']
- c = cookiejar_from_cookie_headers(headers)
- cookie = c._cookies['www.example.com']['/']['c']
- self.assert_(cookie.expires is None)
-
-
-
- class LWPCookieTests(TestCase):
-
- def test_netscape_example_1(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- Request = Request
- import urllib2
- year_plus_one = time.localtime()[0] + 1
- headers = []
- c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
- req = Request('http://www.acme.com:80/', headers = {
- 'Host': 'www.acme.com:80' })
- headers.append('Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/ ; expires=Wednesday, 09-Nov-%d 23:12:40 GMT' % year_plus_one)
- res = FakeResponse(headers, 'http://www.acme.com/')
- c.extract_cookies(res, req)
- req = Request('http://www.acme.com/')
- c.add_cookie_header(req)
- self.assertEqual(req.get_header('Cookie'), 'CUSTOMER=WILE_E_COYOTE')
- self.assertEqual(req.get_header('Cookie2'), '$Version="1"')
- headers.append('Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/')
- res = FakeResponse(headers, 'http://www.acme.com/')
- c.extract_cookies(res, req)
- req = Request('http://www.acme.com/foo/bar')
- c.add_cookie_header(req)
- h = req.get_header('Cookie')
- if 'PART_NUMBER=ROCKET_LAUNCHER_0001' in h:
- pass
- self.assert_('CUSTOMER=WILE_E_COYOTE' in h)
- headers.append('Set-Cookie: SHIPPING=FEDEX; path=/foo')
- res = FakeResponse(headers, 'http://www.acme.com')
- c.extract_cookies(res, req)
- req = Request('http://www.acme.com/')
- c.add_cookie_header(req)
- h = req.get_header('Cookie')
- if 'PART_NUMBER=ROCKET_LAUNCHER_0001' in h and 'CUSTOMER=WILE_E_COYOTE' in h:
- pass
- self.assert_('SHIPPING=FEDEX' not in h)
- req = Request('http://www.acme.com/foo/')
- c.add_cookie_header(req)
- h = req.get_header('Cookie')
- if 'PART_NUMBER=ROCKET_LAUNCHER_0001' in h and 'CUSTOMER=WILE_E_COYOTE' in h:
- pass
- self.assert_(h.startswith('SHIPPING=FEDEX;'))
-
-
- def test_netscape_example_2(self):
- CookieJar = CookieJar
- import cookielib
- Request = Request
- import urllib2
- c = CookieJar()
- headers = []
- req = Request('http://www.acme.com/')
- headers.append('Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/')
- res = FakeResponse(headers, 'http://www.acme.com/')
- c.extract_cookies(res, req)
- req = Request('http://www.acme.com/')
- c.add_cookie_header(req)
- self.assertEquals(req.get_header('Cookie'), 'PART_NUMBER=ROCKET_LAUNCHER_0001')
- headers.append('Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo')
- res = FakeResponse(headers, 'http://www.acme.com/')
- c.extract_cookies(res, req)
- req = Request('http://www.acme.com/ammo')
- c.add_cookie_header(req)
- self.assert_(re.search('PART_NUMBER=RIDING_ROCKET_0023;\\s*PART_NUMBER=ROCKET_LAUNCHER_0001', req.get_header('Cookie')))
-
-
- def test_ietf_example_1(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
- cookie = interact_2965(c, 'http://www.acme.com/acme/login', 'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
- self.assert_(not cookie)
- cookie = interact_2965(c, 'http://www.acme.com/acme/pickitem', 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"')
- self.assert_(re.search('^\\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \\$Path="/acme"$', cookie))
- cookie = interact_2965(c, 'http://www.acme.com/acme/shipping', 'Shipping="FedEx"; Version="1"; Path="/acme"')
- self.assert_(re.search('^\\$Version="?1"?;', cookie))
- self.assert_(re.search('Part_Number="?Rocket_Launcher_0001"?;\\s*\\$Path="\\/acme"', cookie))
- self.assert_(re.search('Customer="?WILE_E_COYOTE"?;\\s*\\$Path="\\/acme"', cookie))
- cookie = interact_2965(c, 'http://www.acme.com/acme/process')
- if re.search('Shipping="?FedEx"?;\\s*\\$Path="\\/acme"', cookie):
- pass
- self.assert_('WILE_E_COYOTE' in cookie)
-
-
- def test_ietf_example_2(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
- interact_2965(c, 'http://www.acme.com/acme/ammo/specific', 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"', 'Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"')
- cookie = interact_2965(c, 'http://www.acme.com/acme/ammo/...')
- self.assert_(re.search('Riding_Rocket_0023.*Rocket_Launcher_0001', cookie))
- cookie = interact_2965(c, 'http://www.acme.com/acme/parts/')
- if 'Rocket_Launcher_0001' in cookie:
- pass
- self.assert_('Riding_Rocket_0023' not in cookie)
-
-
- def test_rejection(self):
- DefaultCookiePolicy = DefaultCookiePolicy
- LWPCookieJar = LWPCookieJar
- import cookielib
- pol = DefaultCookiePolicy(rfc2965 = True)
- c = LWPCookieJar(policy = pol)
- max_age = 'max-age=3600'
- cookie = interact_2965(c, 'http://www.acme.com', 'foo=bar; domain=".com"; version=1')
- self.assert_(not c)
- cookie = interact_2965(c, 'http://www.acme.com', 'ping=pong; domain="acme.com"; version=1')
- self.assertEquals(len(c), 1)
- cookie = interact_2965(c, 'http://www.a.acme.com', 'whiz=bang; domain="acme.com"; version=1')
- self.assertEquals(len(c), 1)
- cookie = interact_2965(c, 'http://www.a.acme.com', 'wow=flutter; domain=".a.acme.com"; version=1')
- self.assertEquals(len(c), 2)
- cookie = interact_2965(c, 'http://125.125.125.125', 'zzzz=ping; domain="125.125.125"; version=1')
- self.assertEquals(len(c), 2)
- cookie = interact_2965(c, 'http://www.sol.no', 'blah=rhubarb; domain=".sol.no"; path="/foo"; version=1')
- self.assertEquals(len(c), 2)
- cookie = interact_2965(c, 'http://www.sol.no/foo/bar', 'bing=bong; domain=".sol.no"; path="/foo"; version=1')
- self.assertEquals(len(c), 3)
- cookie = interact_2965(c, 'http://www.sol.no', 'whiz=ffft; domain=".sol.no"; port="90,100"; version=1')
- self.assertEquals(len(c), 3)
- cookie = interact_2965(c, 'http://www.sol.no', 'bang=wallop; version=1; domain=".sol.no"; port="90,100, 80,8080"; max-age=100; Comment = "Just kidding! (\\"|\\\\\\\\) "')
- self.assertEquals(len(c), 4)
- cookie = interact_2965(c, 'http://www.sol.no', 'foo9=bar; version=1; domain=".sol.no"; port; max-age=100;')
- self.assertEquals(len(c), 5)
- cookie = interact_2965(c, 'http://www.sol.no/<oo/', 'foo8=bar; version=1; path="/%3coo"')
- self.assertEquals(len(c), 6)
- filename = test_support.TESTFN
-
- try:
- c.save(filename, ignore_discard = True)
- old = repr(c)
- c = LWPCookieJar(policy = pol)
- c.load(filename, ignore_discard = True)
- finally:
-
- try:
- os.unlink(filename)
- except OSError:
- pass
-
-
- self.assertEquals(old, repr(c))
-
-
- def test_url_encoding(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
- interact_2965(c, 'http://www.acme.com/foo%2f%25/%3c%3c%0Anew%E5/%E5', 'foo = bar; version = 1')
- cookie = interact_2965(c, 'http://www.acme.com/foo%2f%25/<<%0anew\xe5/\xe6\xf8\xe5', 'bar=baz; path="/foo/"; version=1')
- version_re = re.compile('^\\$version=\\"?1\\"?', re.I)
- if 'foo=bar' in cookie:
- pass
- self.assert_(version_re.search(cookie))
- cookie = interact_2965(c, 'http://www.acme.com/foo/%25/<<%0anew\xe5/\xe6\xf8\xe5')
- self.assert_(not cookie)
- cookie = interact_2965(c, u'http://www.acme.com/├╝')
-
-
- def test_mozilla(self):
- MozillaCookieJar = MozillaCookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- year_plus_one = time.localtime()[0] + 1
- filename = test_support.TESTFN
- c = MozillaCookieJar(filename, policy = DefaultCookiePolicy(rfc2965 = True))
- interact_2965(c, 'http://www.acme.com/', 'foo1=bar; max-age=100; Version=1')
- interact_2965(c, 'http://www.acme.com/', 'foo2=bar; port="80"; max-age=100; Discard; Version=1')
- interact_2965(c, 'http://www.acme.com/', 'foo3=bar; secure; Version=1')
- expires = 'expires=09-Nov-%d 23:12:40 GMT' % (year_plus_one,)
- interact_netscape(c, 'http://www.foo.com/', 'fooa=bar; %s' % expires)
- interact_netscape(c, 'http://www.foo.com/', 'foob=bar; Domain=.foo.com; %s' % expires)
- interact_netscape(c, 'http://www.foo.com/', 'fooc=bar; Domain=www.foo.com; %s' % expires)
-
- def save_and_restore(cj, ignore_discard):
-
- try:
- cj.save(ignore_discard = ignore_discard)
- new_c = MozillaCookieJar(filename, DefaultCookiePolicy(rfc2965 = True))
- new_c.load(ignore_discard = ignore_discard)
- finally:
-
- try:
- os.unlink(filename)
- except OSError:
- pass
-
-
- return new_c
-
- new_c = save_and_restore(c, True)
- self.assertEquals(len(new_c), 6)
- self.assert_("name='foo1', value='bar'" in repr(new_c))
- new_c = save_and_restore(c, False)
- self.assertEquals(len(new_c), 4)
- self.assert_("name='foo1', value='bar'" in repr(new_c))
-
-
- def test_netscape_misc(self):
- CookieJar = CookieJar
- import cookielib
- Request = Request
- import urllib2
- c = CookieJar()
- headers = []
- req = Request('http://foo.bar.acme.com/foo')
- headers.append('Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.com')
- res = FakeResponse(headers, 'http://www.acme.com/foo')
- c.extract_cookies(res, req)
- headers.append('Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.com')
- res = FakeResponse(headers, 'http://www.acme.com/foo')
- c.extract_cookies(res, req)
- req = Request('http://foo.bar.acme.com/foo')
- c.add_cookie_header(req)
- if 'PART_NUMBER=3,4' in req.get_header('Cookie'):
- pass
- self.assert_('Customer=WILE_E_COYOTE' in req.get_header('Cookie'))
-
-
- def test_intranet_domains_2965(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
- interact_2965(c, 'http://example/', 'foo1=bar; PORT; Discard; Version=1;')
- cookie = interact_2965(c, 'http://example/', 'foo2=bar; domain=".local"; Version=1')
- self.assert_('foo1=bar' in cookie)
- interact_2965(c, 'http://example/', 'foo3=bar; Version=1')
- cookie = interact_2965(c, 'http://example/')
- if 'foo2=bar' in cookie:
- pass
- self.assert_(len(c) == 3)
-
-
- def test_intranet_domains_ns(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- c = CookieJar(DefaultCookiePolicy(rfc2965 = False))
- interact_netscape(c, 'http://example/', 'foo1=bar')
- cookie = interact_netscape(c, 'http://example/', 'foo2=bar; domain=.local')
- self.assertEquals(len(c), 2)
- self.assert_('foo1=bar' in cookie)
- cookie = interact_netscape(c, 'http://example/')
- self.assert_('foo2=bar' in cookie)
- self.assertEquals(len(c), 2)
-
-
- def test_empty_path(self):
- CookieJar = CookieJar
- DefaultCookiePolicy = DefaultCookiePolicy
- import cookielib
- Request = Request
- import urllib2
- c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
- headers = []
- req = Request('http://www.ants.com/')
- headers.append('Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=')
- res = FakeResponse(headers, 'http://www.ants.com/')
- c.extract_cookies(res, req)
- req = Request('http://www.ants.com/')
- c.add_cookie_header(req)
- self.assertEquals(req.get_header('Cookie'), 'JSESSIONID=ABCDERANDOM123')
- self.assertEquals(req.get_header('Cookie2'), '$Version="1"')
- req = Request('http://www.ants.com:8080')
- c.add_cookie_header(req)
- self.assertEquals(req.get_header('Cookie'), 'JSESSIONID=ABCDERANDOM123')
- self.assertEquals(req.get_header('Cookie2'), '$Version="1"')
-
-
- def test_session_cookies(self):
- CookieJar = CookieJar
- import cookielib
- Request = Request
- import urllib2
- year_plus_one = time.localtime()[0] + 1
- req = Request('http://www.perlmeister.com/scripts')
- headers = []
- headers.append('Set-Cookie: s1=session;Path=/scripts')
- headers.append('Set-Cookie: p1=perm; Domain=.perlmeister.com;Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMT' % year_plus_one)
- headers.append('Set-Cookie: p2=perm;Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMT' % year_plus_one)
- headers.append('Set-Cookie: s2=session;Path=/scripts;Domain=.perlmeister.com')
- headers.append('Set-Cookie2: s3=session;Version=1;Discard;Path="/"')
- res = FakeResponse(headers, 'http://www.perlmeister.com/scripts')
- c = CookieJar()
- c.extract_cookies(res, req)
- counter = {
- 'session_after': 0,
- 'perm_after': 0,
- 'session_before': 0,
- 'perm_before': 0 }
- for cookie in c:
- key = '%s_before' % cookie.value
- counter[key] = counter[key] + 1
-
- c.clear_session_cookies()
- for cookie in c:
- key = '%s_after' % cookie.value
- counter[key] = counter[key] + 1
-
- if not counter['perm_after'] != counter['perm_before'] and counter['session_after'] != 0:
- pass
- self.assert_(not (counter['session_before'] == 0))
-
-
-
- def test_main(verbose = None):
- test_sets = test_sets
- import test
- test_support.run_unittest(DateTimeTests, HeaderTests, CookieTests, FileCookieJarTests, LWPCookieTests)
-
- if __name__ == '__main__':
- test_main(verbose = True)
-
-